﻿using BLL;
using CommonLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectStudent
{
    public partial class UserManage : Form
    {

        public UserManage()
        {
            InitializeComponent();
            // 清空所有列，防止重复
            dataGridView1.Columns.Clear();
            // 禁止自动生成列
            dataGridView1.AutoGenerateColumns = false;

            // 用户名列
            var colUserName = new DataGridViewTextBoxColumn();
            colUserName.HeaderText = "用户名";
            colUserName.DataPropertyName = "Name"; // 数据源字段名
            dataGridView1.Columns.Add(colUserName);

            // 密码列
            var colPassword = new DataGridViewTextBoxColumn();
            colPassword.HeaderText = "密码";
            colPassword.DataPropertyName = "Password";
            dataGridView1.Columns.Add(colPassword);

            // 创建时间列
            var colTime = new DataGridViewTextBoxColumn();
            colTime.HeaderText = "创建时间";
            colTime.DataPropertyName = "time";
            dataGridView1.Columns.Add(colTime);
            LoadUserPage();
            

        }
        UserBLL userBLL = new UserBLL();
        private void LoadUser()
        {
            DataTable dt = userBLL.GetUserList();
            dataGridView1.DataSource = dt;
        }

        private string currentKeyword = "";
        private void LoadUserPage()
        {
            int total = userBLL.UserCount(currentKeyword);
            lb_total.Text = total.ToString();

            int pageSize = Convert.ToInt32(comboBox1.Text);
            int totalPages = (int)Math.Ceiling((double)total / pageSize);
            lb_pageCount.Text = totalPages.ToString();

            int pageNumber = int.Parse(txtCurrentPage.Text);
            DataTable dataTable = userBLL.GetUserPage(pageNumber, pageSize, currentKeyword);
            dataGridView1.DataSource = dataTable;
        }


        private void btnPageSearch_Click(object sender, EventArgs e)
        {
            
            LoadUser();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadUserPage();
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            txtCurrentPage.Text = "1";
            LoadUserPage();
        }

        private void btnPre_Click(object sender, EventArgs e)
        {
            int pageNumber = int.Parse(txtCurrentPage.Text);
            if (pageNumber > 1)
            {
                pageNumber--;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是第一页了");
            }
            LoadUserPage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int pageNumber = int.Parse(txtCurrentPage.Text);
            int totalPages = int.Parse(lb_pageCount.Text);
            if (pageNumber < totalPages)
            {
                pageNumber++;
                txtCurrentPage.Text = pageNumber.ToString();
            }
            else
            {
                MessageBox.Show("已经是最后一页了");
            }
            LoadUserPage();
        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            int totalPages = int.Parse(lb_pageCount.Text);

            txtCurrentPage.Text = totalPages.ToString();
            LoadUserPage();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserEditForm userEdit = new UserEditForm(null);
            if (userEdit.ShowDialog() == DialogResult.OK)
            {

                if (userBLL.AddUser(userEdit.UserName, userEdit.Password, userEdit.time))
                {
                    MessageBox.Show("新增成功！");
                    LoadUser();
                }
                else
                {
                    MessageBox.Show("新增失败！");
                }

            }

        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请先选择要编辑的用户！");
                return;
            }
            DataRowView rowView = dataGridView1.CurrentRow.DataBoundItem as DataRowView;
            if (rowView == null)
            {
                MessageBox.Show("数据绑定错误！");
                return;
            }
            UserEditForm userEdit = new UserEditForm(rowView.Row);
            if (userEdit.ShowDialog() == DialogResult.OK)
            {
                if (userBLL.UpdateUser(userEdit.UserId, userEdit.UserName, userEdit.Password, userEdit.time))
                {
                    MessageBox.Show("编辑成功！");
                    LoadUser();
                }
                else
                {
                    MessageBox.Show("编辑失败！");
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show("请先选择要删除的用户！");
                return;
            }
            DataRow rowView = dataGridView1.CurrentRow.DataBoundItem as DataRow;
            if (rowView == null)
            {
                MessageBox.Show("数据绑定错误！");
                return;
            }
            int id = Convert.ToInt32(rowView["Id"]);
            if (MessageBox.Show("确认要删除该用户吗？", "确认删除", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (userBLL.DeleteUser(id))
                {
                    MessageBox.Show("删除成功！");
                    LoadUser();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadUser();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = textBox1.Text.Trim();
            if (string.IsNullOrEmpty(keyword))
            {
                LoadUserPage();    
                return;
            }
            DataTable dt = userBLL.SearchHaveKeyWord(keyword);
            if (dt .Rows.Count> 0)
            {
                
                // 绑定到dataGridView1
                dataGridView1.DataSource = dt;

            }
            else
            {
                dataGridView1.DataSource = null;
                //MessageBox.Show("未找到包含该关键字的用户！");

            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "CSV文件 (*.csv)|*.csv";
            openFileDialog.Title = "请选择要导入的CSV文件";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                if (userBLL.ImportUserFromCsv(filePath))
                {
                    MessageBox.Show("导入成功！");
                    LoadUser();
                }
                else
                {
                    MessageBox.Show("导入失败！");
                }
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "CSV文件 (*.csv)|*.csv";
            saveFileDialog.Title = "请选择导出CSV文件保存位置";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
                if (userBLL.ExportUserToCsv(filePath))
                {
                    MessageBox.Show("导出成功！");
                }
                else
                {
                    MessageBox.Show("导出失败！");
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel6_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
